package com.woniu108.order.consumer;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu108.carService.commos.dto.UserCartDto;
import com.woniu108.carService.commos.util.RedisKey;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.order.client.ProductClient;
import com.woniu108.order.mapper.OrderDeleteMapper;
import com.woniu108.order.mapper.OrderProductMapper;
import com.woniu108.order.model.model.OrderDelete;
import com.woniu108.order.model.model.OrderProduct;
import com.woniu108.product.form.FailedSaleForm;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author LY
 */
@Component
@RabbitListener(queues = "order_product_del_queue")
public class OrderProductDelConsumer {
    @Resource
    private OrderProductMapper orderProductMapper;
    @Resource
    private ProductClient productClient;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private OrderDeleteMapper orderDeleteMapper;

    @RabbitHandler
    public void DeleteOrderProductByNum(String orderProductNum){
        System.out.println("==="+orderProductNum+"===");
        OrderProduct orderProduct = orderProductMapper.selectOne(new QueryWrapper<OrderProduct>().eq("order_product_num",orderProductNum));
        System.out.println("+++"+orderProduct);
        if(orderProduct.getOrderProductState().equals(StaticData.ORDER_PRODUCT_STATE_NO_PAID)){
            orderProduct.setOrderProductState(StaticData.ORDER_PRODUCT_STATE_CANCELED);
            orderProductMapper.updateById(orderProduct);

            //添加理由
            OrderDelete orderDelete = new OrderDelete();
            orderDelete.setOrderProductNum(orderProductNum);
            orderDelete.setDeleteReason("长时间未支付，已过期");
            orderDelete.setReason("长时间未支付，已过期");
            orderDelete.setDeleteTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new DateTime()));
            orderDelete.setDealTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new DateTime()));
            orderDelete.setDeleteState(1);
            orderDeleteMapper.insert(orderDelete);

            ArrayList<Map<Object, Object>> maps = new ArrayList<>();
            Set<Object> set = redisUtil.sMembers(RedisKey.orderProductPInfoSet(orderProductNum));
            for (Object s:set){
                Integer pInfoId=Integer.parseInt(s.toString());
                Map<Object, Object> map = redisUtil.hgetall(RedisKey.orderProductPInfoByNumHash(orderProductNum, pInfoId));
                maps.add(map);
            }
            for (Map<Object,Object> map:maps){
                FailedSaleForm form = new FailedSaleForm();
                form.setPInfoId(Integer.valueOf(map.get("pInfoId").toString()) );
                form.setNum(Integer.valueOf(map.get("number").toString()) );
                productClient.failedSale(form);
                productClient.submitFreezeNum(form);
            }
        }
    }
}
